package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Tipodocumento;
import services.TipoDocumentoService;

@FacesConverter("tipoDocumentoConverter")
public class TipoDocumentoConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
        	TipoDocumentoService service = (TipoDocumentoService) fc.getExternalContext().getApplicationMap().get("tipoDocumentoService");
            Iterator<Tipodocumento> i=service.getListaTipoDocumento().iterator();
            while (i.hasNext()) {
            	Tipodocumento t = (Tipodocumento) i.next();
				if(t.getIdTipoDocumento() == Integer.parseInt(value))
					return t;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Tipodocumento) object).getIdTipoDocumento());
        }
        else {
            return null;
        }
    }  
	
}
